﻿using Platformer_Example.Controller.Input;
using Platformer_Example.Screen.Common;
using Platformer_Example.Screen.QuickGame;
using System.Collections.Generic;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Global;
using Transformable_Engine_v2.Engine.Input;

namespace Platformer_Example
{
    public class Platformer : Container
    {
        private InputTransmitter _input;

        private BaseScreen _menu;

        public override void LoadContent()
        {
            KeyboardHandler.OnKeyboardPress += OnKeyPress;
            KeyboardHandler.OnKeyboardReleased += OnKeyRelease;
            GamepadHandler.OnButtonPressed += OnGamepadPress;
            GamepadHandler.OnButtonReleased += OnGamepadRelease;

            _input = new InputTransmitter(new InputState(),
                                          new Dictionary<KeyboardInput.Key, InputState.Key>
                                              {
                                                  {KeyboardInput.Key.W, InputState.Key.Up},
                                                  {KeyboardInput.Key.S, InputState.Key.Down},
                                                  {KeyboardInput.Key.A, InputState.Key.Left},
                                                  {KeyboardInput.Key.D, InputState.Key.Right},
                                                  {KeyboardInput.Key.F, InputState.Key.ButtonNorth},
                                                  {KeyboardInput.Key.V, InputState.Key.ButtonSouth},
                                                  {KeyboardInput.Key.C, InputState.Key.ButtonWest},
                                                  {KeyboardInput.Key.B, InputState.Key.ButtonEast},
                                              });

            ChangeScreen(new QuickGame());

            base.LoadContent();
        }

        void ChangeScreen(BaseScreen nextMenu)
        {
            Log.LogDebug("Change screen to " + nextMenu);

            if (_menu != null)
            {
                _menu.Die();
                _menu.OnChangeScreen -= ChangeScreen;
            }

            _menu = nextMenu;

            _menu.OnChangeScreen += ChangeScreen;
            AddChildren(_menu);
        }

        void OnKeyPress(KeyboardInput input)
        {
            var key = _input.OnKeyPress(-1, input.KeyCode);
            if (key == InputState.Key.Invalid)
            {
                _menu.InvalidKeyPressed(input.KeyCode);
            }
            else
            {
                _menu.Keypressed(key);
            }
        }

        void OnKeyRelease(KeyboardInput input)
        {
            var key = _input.OnKeyRelease(-1, input.KeyCode);
            if(key != InputState.Key.Invalid)
                _menu.KeyReleased(key);
        }

        void OnGamepadPress(uint joystickId, KeyboardInput.Key input)
        {
            _input.OnKeyPress((int)joystickId, input);
        }

        void OnGamepadRelease(uint joystickId, KeyboardInput.Key input)
        {
            _input.OnKeyRelease((int)joystickId, input);
        }
    }
}